#include <iostream>
#include <fstream>

#include "PGPLOT_Renderer.h"
#include "DataContainer.h"

#include "cpgplot.h"

PGPLOT_Renderer::PGPLOT_Renderer() {

	cout << "PGPLOT_Renderer::PGPLOT_Renderer Default Constructor" << endl;
	
	this->device = NULL;

	xmin=0.0;
	xmax=1.0;
	ymin=0;
	ymax=1.0;

	title = "PGPLOT BLANK FRAME";
	xlabel = "X Axis";
	ylabel = "Y Axis";

	x_options = "ABCNST";
	y_options = "ABCNST";

	num_levels = 64;


}

PGPLOT_Renderer::~PGPLOT_Renderer() {

	cout << "PGPLOT_Renderer::~PGPLOT_Renderer Default Destructor" << endl;

}
void PGPLOT_Renderer::set_device(char *dev) {

	device = strdup(dev);

}
void PGPLOT_Renderer::set_numlevels(int nl) {

	num_levels = nl;
}
int PGPLOT_Renderer::init() {

	
	if (device == NULL) {
		cout << "PGPLOT_Renderer::init using default device " << DEFAULT_DEV << endl;
		set_device((char *) DEFAULT_DEV);

	}

	cpgopen(device);

 	return EXIT_SUCCESS;

}
void PGPLOT_Renderer::set_window(float x1, float x2, float y1, float y2) {

	xmin=x1;
	xmax=x2;
	ymin=y1;
	ymax=y2;

	cpgswin(xmin,xmax,ymin,ymax);

}
void PGPLOT_Renderer::set_labels(string t, string xl, string yl) {

	title = t;
	xlabel=xl;
	ylabel=yl;
}	
int PGPLOT_Renderer::draw_frame() {

	cout << "PGPLOT_Renderer::draw() Drawing Member Function" << endl;
 	cpgswin(xmin,xmax,ymin,ymax);
	cpgbox(x_options.c_str(),0.0,0.0,y_options.c_str(),0.0,0.0);
	cpglab(xlabel.c_str(),ylabel.c_str(),title.c_str());

	return EXIT_SUCCESS;
		
}
int PGPLOT_Renderer::draw_pixels(DataContainer *container) {

	// First need to get the number of pixels

	int npix = container->ndata;

	if (npix > 0) {

		cout << "Will attempt to render " << npix << " pixels onto device" << endl;
		return EXIT_SUCCESS;
	}
	else {
		cout << "Container has no pixels (perhaps a function?) " << endl;
	}

	return EXIT_FAILURE;
	
}
	
void PGPLOT_Renderer::draw_pixel(float x1, float x2, float y1, float y2, float value) {
}

